//
//  SEGDailySubjectModel.m
//  SEGUhomeSommunitySocial
//
//  Created by 揭婧雯 on 2019/4/11.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGDailySubjectModel.h"

@implementation SEGDailySubjectModel

- (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    self = [super init];
    if (self) {
        _topicName = @"";
        _holidayName = @"";
        _topicId = @"";
        _onlineTime = @"";
        _relevanceNum = @"";
        [self setValuesForKeysWithDictionary:dictionary];
    }
    return self;
}
+ (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    return [[self alloc] initWithDictionary:dictionary];
}
- (void)setValue:(id)value forUndefinedKey:(NSString*)key
{}
- (void)setTopicName:(NSString*)topicName{
    _topicName = [[SEGUtils ValueToString:topicName]copy];
}
- (void)setHolidayName:(NSString*)holidayName{
    _holidayName = [[SEGUtils ValueToString:holidayName]copy];
}
- (void)setTopicId:(NSString*)topicId{
    _topicId = [[SEGUtils ValueToString:topicId]copy];
}

- (void)setRelevanceNum:(NSString*)relevanceNum{
    _relevanceNum = [[SEGUtils ValueToString:relevanceNum]copy];
}

- (void)setOnlineTime:(NSString*)onlineTime{
    _onlineTime = [[SEGUtils ValueToString:onlineTime] copy];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[onlineTime doubleValue]];
    _weekString = [self weekdayStringFromDate:date];
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"dd MM yyyy";
    NSString* timeStr = [dateFormatter stringFromDate:date];
    
    _dayString = [timeStr substringToIndex:2];
    
    NSString *month = [timeStr substringWithRange:NSMakeRange(3, 2)];
    NSString *monthString = [self monthAbbreviationsStringFromInteger:[month integerValue]];
    _monthYearString = [NSString stringWithFormat:@"%@%@", monthString, [timeStr substringWithRange:NSMakeRange(6, 4)]];
}

- (NSString*)weekdayStringFromDate:(NSDate*)inputDate
{
    // 在使用[dateComponents weekday]获取某天对应的星期时，会发现，星期日对应的返回值是1，周一对应的值是2，依次递推，周六对应的值是7
    NSArray* weekdays = [NSArray arrayWithObjects:[NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSTimeZone* timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Beijing"];
    
    [calendar setTimeZone:timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents* theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];
}

- (NSString*)monthAbbreviationsStringFromInteger:(NSInteger)integer
{
    NSArray* monthArray = [NSArray arrayWithObjects:[NSNull null], @"Jan.", @"Feb.", @"Mar.", @"Apr.", @"May.", @"Jun.", @"Jul.",@"Aug.", @"Sep.", @"Oct.", @"Nov.", @"Dec.", nil];
    if (integer < monthArray.count) {
        return monthArray[integer];
    }else {
        return @"";
    }
}

@end
